<?php

namespace Biz\service\bizAd;

use Biz\model\brand\Brand as mBrand;
use Biz\model\store\StoreBasic as mStoreBasic;
use Biz\pockets\constInterface\BizAd as iBizAd;
use Doraemon\service\User;

class AdUser
{
    /**
     * 格式化用户信息
     * @param $uids
     * @return array
     */
    public static function formatUserInfo($uids)
    {
        $users = User::getBatchBasicUserInfo($uids);

        $userGroup = [];

        foreach ($users as $user) {
            $userGroup[$user['type']][] = $user['uid'];
        }

        $brandsMap = $storeMap = $userMap = [];

        foreach ($userGroup as $utype => $group) {
            switch ($utype) {
                case iBizAd::UTYPE_BRAND:
                    $brands = mBrand::select(['id', 'brand_name', 'uid'], ['uid' => $group]);
                    $brandsMap = empty($brands) ? [] : array_column($brands, null, 'uid');
                    break;
                case iBizAd::UTYPE_STORE:
                    $stores = mStoreBasic::select(['id', 'name', 'uid'], ['uid' => $group]);
                    $storeMap = empty($brands) ? [] : array_column($stores, null, 'uid');
                    break;
                case iBizAd::UTYPE_DESIGNER:
                case iBizAd::UTYPE_COMMMON:
                    $userMap = array_column($users, null, 'uid');
                    break;
            }
        }

        $formatData = [];

        foreach ($users as $user) {
            switch ($user['type']) {
                case iBizAd::UTYPE_BRAND:
                    $formatData[$user['uid']] = [
                        'nick' => $user['nick'],
                        'uid' => $user['uid'],
                        'bname' => isset($brandsMap[$user['uid']]) ? $brandsMap[$user['uid']]['brand_name'] : '',
                        'bid' => isset($brandsMap[$user['uid']]) ? $brandsMap[$user['uid']]['id'] : '',
                    ];
                    break;
                case iBizAd::UTYPE_STORE:
                    $formatData[$user['uid']] = [
                        'nick' => $user['nick'],
                        'uid' => $user['uid'],
                        'bname' => isset($storeMap[$user['uid']]) ? $storeMap[$user['uid']]['name'] : '',
                        'bid' => isset($storeMap[$user['uid']]) ? $storeMap[$user['uid']]['id'] : '',
                    ];
                    break;
                case iBizAd::UTYPE_DESIGNER:
                case iBizAd::UTYPE_COMMMON:
                    $formatData[$user['uid']] = [
                        'nick' => $user['nick'],
                        'uid' => $user['uid'],
                        'bname' => $user['nick'],
                        'bid' => $user['uid'],
                    ];
                    break;
            }
        }

        return $formatData;

    }
}